
import java.util.Iterator;
import java.util.Stack;


public class PreOrderIterator implements Iterator {

	private Stack stack;

	public PreOrderIterator( BinaryTreeNode root ) {
		stack = new Stack();
		stack.push( root );
	}

	public boolean hasNext() {
		return !stack.isEmpty();
	}

	public Object next() {
		if (!hasNext()) {
			throw new IllegalStateException();
		}

		BinaryTreeNode node = (BinaryTreeNode)stack.pop();

		if (node.right != null) {
			stack.push( node.right );
		}

		if (node.left != null) {
			stack.push( node.left );
		}

		return node;
	}

	public void remove() {
		throw new UnsupportedOperationException();
	}

}

